// GUI

SS.GUI       = {}
SS.GUI.Panel = {}
SS.GUI.Font  = "ServerSecureVerdana"

// Play sound

function SS.GUI.PlaySound(Message)
	local String = Message:ReadString()
	
	surface.PlaySound(String)
end

usermessage.Hook("SS.GUI.PlaySound", SS.GUI.PlaySound)

// Player message

function SS.GUI.PlayerMessage(Message)
	local T = Message:ReadShort()
	local M = Message:ReadString()
	
	local Sound = "ambient/water/drip2.wav"
	
	if T == 1 then
		Sound = "buttons/button10.wav"
	elseif T == 2 then
		Sound = "buttons/button17.wav"
	elseif T == 3 then
		Sound = "buttons/bell1.wav"
	elseif T == 4 then
		Sound = "ambient/machines/slicer"..math.random(1, 4)..".wav"
	end
	
	GAMEMODE:AddNotify(M, T, 10) 
	
	surface.PlaySound(Sound)
end

usermessage.Hook("SS.PlayerMessage", SS.GUI.PlayerMessage)

// HUDPaint

function SS.GUI.Update()
	local TR = utilx.GetPlayerTrace(SS.Client, SS.Client:GetCursorAimVector())
	
	local Trace = util.TraceLine(TR)
	
	local Players = player.GetAll()
	
	for K, V in pairs(Players) do
		local Living = V:Alive()
		
		if (Living) then
			local Person = V
			
			if (Person != SS.Client) then
				local Pos = Person:GetShootPos() - SS.Client:GetShootPos()
				local Leng = Pos:Length()
				
				// Config
				
				local Fade = SS.Config.Request("Name Fading Distance", "Number", -1)
				
				// Check
				
				if (Leng < Fade or Fade == -1) then
					if (Leng < 128) then
						Leng = 128
					end
					
					local Alpha = math.max(math.min(255 - ((255 / Fade) * Leng), 255), 0)
					
					if (Fade == -1) then Alpha = 255 end
					
					local X = Person:GetShootPos():ToScreen().x - 6
					local Y = Person:GetShootPos():ToScreen().y + 100
					
					local Col = team.GetColor(Person:Team())
					
					if not (SS.Lib.HiddenGUI(Person, "Name")) then
						local ID = Person:Name()
						
						draw.SimpleText(ID, SS.GUI.Font, X + 1, (Y - 178) + 1, Color(0, 0, 0, Alpha), 1, 1)
						draw.SimpleText(ID, SS.GUI.Font, X, (Y - 178), Color(Col.r, Col.g, Col.b, Alpha), 1, 1)
						
						local NY   = (Y - 178)
						local Cur = 0
						
						local Data = SS.Parts.Request(Person, "Name")
						
						for B, J in pairs(Data) do
							if (J != "") then
								Cur = Cur + 16
								
								draw.SimpleText(J, SS.GUI.Font, X + 1, (NY + Cur) + 1, Color(0, 0, 0, Alpha), 1, 1)
								draw.SimpleText(J, SS.GUI.Font, X, NY + Cur, Color(255, 255, 255, Alpha), 1, 1)
							end
						end
					end
					
					if not (SS.Lib.HiddenGUI(Person, "Hover")) then
						if (Trace.Entity) then
							if (Trace.Entity == Person) then
								local HX = gui.MouseX() - 16
								local HY = gui.MouseY() - 64
								
								local Cur = 0
								
								local Number = 0
								
								local Amount = 0
								
								local Data = SS.Parts.Request(Person, "Hover")
								
								// Font
								
								surface.SetFont(SS.GUI.Font)
								
								for B, J in pairs(Data) do
									if (J != "") then
										Number = Number + 1
										
										local W, H = surface.GetTextSize(J)
										
										if (W > Amount) then
											Amount = W
										end
									end
								end
								
								local BX, BY, BW, BH = (HX - 48), HY, (Amount + 32), ((18 * Number) + 10)
								
								SS.GUI.Box(BX, BY, BW, BH, Color(50, 50, 50, Alpha))
								
								local Data = SS.Parts.Request(Person, "Hover")
								
								for B, J in pairs(Data) do
									if (J != "") then
										Cur = Cur + 16
										
										draw.SimpleText(J, SS.GUI.Font, (BX + (BW / 2)) + 1, (BY + Cur) + 1, Color(0, 0, 0, Alpha), 1, 1)
										draw.SimpleText(J, SS.GUI.Font, BX + (BW / 2), (BY + Cur), Color(255, 255, 255, Alpha), 1, 1)
									end
								end
							end
						end
					end
				end
			end
		end
	end
end

hook.Add("HUDPaint", "SS.GUI.Update", SS.GUI.Update)

// Draw a box

function SS.GUI.Box(X, Y, W, H, Col)
	draw.RoundedBox(4, X, Y, W, H, Col)
	
	return {X = X, Y = Y, W = W, H = H}
end